package com.mobartisan.vehiclenetstore.network.interceptor;

import android.content.Context;


import com.mobartisan.vehiclenetstore.util.StringUtil;

import java.io.IOException;
import java.net.SocketTimeoutException;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/4/12 0012.
 */
public class CookiesInterceptor implements Interceptor {
    private static final String TAG = CookiesInterceptor.class.getSimpleName();
    private Context context;

    public CookiesInterceptor(Context context) {
        this.context = context;
    }

    //重写拦截方法，处理自定义的Cookies信息
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        String jsessionid = "";
        HttpUrl.Builder authorizedUrlBuilder = null;
        if (!StringUtil.isNullOrEmpty(jsessionid)) {
            // 添加新的参数
            authorizedUrlBuilder = request.url()
                    .newBuilder()
                    .scheme(request.url().scheme())
                    .host(request.url().host())
                    .addQueryParameter("jsessionid", jsessionid);
        } else {
            authorizedUrlBuilder = request.url()
                    .newBuilder()
                    .scheme(request.url().scheme())
                    .host(request.url().host());
        }
        Request compressedRequest = request.newBuilder()
                //.header("cookie", CookieUtil.getCookies(context))
                .method(request.method(), request.body())
                .url(authorizedUrlBuilder.build())
                .build();
        try {
            Response response = chain.proceed(compressedRequest);
            return response;
        } catch (SocketTimeoutException e) {
//            Response response=new Response(authorizedUrlBuilder);
        }
        return null;
    }
}
